﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Windows.Media;
using System.Windows.Forms;
using Styx;
using Styx.Helpers;
using Styx.Common;
using Styx.CommonBot.POI;
using Styx.CommonBot;
using Styx.CommonBot.Routines;
using Styx.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using System.Runtime.InteropServices;
using Styx.TreeSharp;
using Action = Styx.TreeSharp.Action;

namespace Freebie
{
    public partial class Classname : CombatRoutine
    {
        public override string Name { get { return "Feral Druid Quick and Dirty by Pasterke"; } }
        public override WoWClass Class { get { return WoWClass.Druid; } }
        LocalPlayer Me { get { return StyxWoW.Me; } }

        private Composite _Combat, _preCombatBuffs, _CombatBuffs;
        public override Composite CombatBehavior { get { using (StyxWoW.Memory.AcquireFrame()) return _Combat; } }
        public override Composite PreCombatBuffBehavior { get { using (StyxWoW.Memory.AcquireFrame()) return _preCombatBuffs; } }
        public override Composite CombatBuffBehavior { get { using (StyxWoW.Memory.AcquireFrame()) return _CombatBuffs; } }
         
        public override bool WantButton { get { return true; } }

        public override void OnButtonPress()
        {
            Form1 ConfigForm = new Form1();
            ConfigForm.ShowDialog();
        }

        public bool RemoveDeadTargets = true;

        public override void Pulse()
        {
            try
            { 
                if (gotTarget
                    && !Me.CurrentTarget.Lootable
                    && Me.CurrentTarget.IsDead
                    && RemoveDeadTargets
                    && Me.CurrentTarget.Distance >= 5f)
                {
                    Me.ClearTarget();
                    return;
                }
                if (gotTarget 
                    && Me.Combat 
                    && Me.CurrentTarget.IsFriendly
                    && !Me.GroupInfo.IsInParty
                    && !Me.GroupInfo.IsInRaid
                    && !Me.IsInInstance
                    && !Me.GroupInfo.IsInBattlegroundParty)
                {
                    Me.ClearTarget();
                    return;
                }
                if (gotTarget 
                    && Me.Combat 
                    && RemoveDeadTargets 
                    && Me.CurrentTarget.IsDead)
                {
                    Me.ClearTarget();
                    LogMsg("Dead Target removed (" + Me.CurrentTarget.Name + ")", 1);
                    return;
                }

            }
            catch { }
        }


        #region Initialize
        public override void Initialize()
        {
            MessageBox.Show(">>> Best results with Combat Bot ! <<<");
            _Combat = createCombat();
            _preCombatBuffs = createPreCombatBuffs();
            _CombatBuffs = createCombatBuffs();
            Updater.CheckForUpdate();
            Lua.Events.AttachEvent("MODIFIER_STATE_CHANGED", HandleModifierStateChanged);
        }
        #endregion

        /* ******* CC_Begin ****** */

        #region precombatbuffs
        public Composite createPreCombatBuffs()
        {
            return new PrioritySelector(
                CastBuff("Mark of the Wild", ret => !HaveStatsBuffs && !buffExists("Mark of the Wild", Me))
                );
        }
        #endregion precombatbuffs

        #region CombatBuffs
        public Composite createCombatBuffs()
        {
            return new PrioritySelector(

                CastBuff("Mark of the Wild", ret => !HaveStatsBuffs && !buffExists("Mark of the Wild", Me)),

                Cast("Cat Form", ret => gotTarget
                    && Me.Specialization == WoWSpec.DruidFeral
                    && Me.Shapeshift != ShapeshiftForm.Cat
                    && !SwitchBearForm),

                Cast("Bear Form", ret => gotTarget
                    && Me.Specialization == WoWSpec.DruidFeral
                    && Me.Shapeshift != ShapeshiftForm.Bear
                    && (SwitchBearForm
                    || (addCount >= CRSettings.myPrefs.startBearAoe && !Me.GroupInfo.IsInRaid && !Me.IsInInstance))),

                new Action(ret => { useAlchemistFlask(); return RunStatus.Failure; }),
                new Action(ret => { useFlask(); return RunStatus.Failure; }),
                new Action(ret => { useHealthStone(); return RunStatus.Failure; }),

                CastBuff("Renewal", ret => gotTarget 
                    && SpellManager.HasSpell("Renewal") 
                    && Me.HealthPercent <= CRSettings.myPrefs.RenewalPercent),

                CastBuff("Healing Touch", ret => gotTarget 
                    && SpellManager.HasSpell("Nature's Swiftness")
                    && buffExists("Nature's Swiftness", Me) 
                    && Me.HealthPercent <= CRSettings.myPrefs.HealingTouchPercent),

                CastBuff("Cenarion Ward", ret => gotTarget
                    && !spellOnCooldown("Cenarion Ward")
                    && Me.HealthPercent <= CRSettings.myPrefs.CenarionWardPercent),
                CastBuff("Barkskin", ret => gotTarget
                    && !spellOnCooldown("Barkskin")
                    && Me.HealthPercent <= CRSettings.myPrefs.BarskinPercent),
                CastBuff("Survival Instincts", ret => gotTarget
                    && !spellOnCooldown("Survival Instincts")
                    && Me.HealthPercent <= CRSettings.myPrefs.SurvivalInstinctsPercent),
                Cast("Tiger's Fury", ret => gotTarget
                    && LastSpell != "Tiger's Fury"
                    && !buffExists("Lifeblood", Me)
                    && !HaveHasteBuff
                    && Me.EnergyPercent <= 25
                    && Me.CurrentTarget.IsWithinMeleeRange),
                Cast("Lifeblood", ret => gotTarget
                    && LastSpell != "Lifeblood"
                    && CRSettings.myPrefs.useLifeblood
                    && SpellManager.HasSpell("Lifeblood")
                    && !buffExists("Tiger's Fury", Me)
                    && !HaveHasteBuff
                    && Me.EnergyPercent <= 25
                    && Me.CurrentTarget.IsWithinMeleeRange),
                Cast("Berserk", ret => gotTarget
                    && LastSpell != "Berserk"
                    && !spellOnCooldown("Berserk")
                    && !HaveHasteBuff
                    && ((CRSettings.myPrefs.useBerserk == 2 && IsWoWBoss(Me.CurrentTarget))
                    || CRSettings.myPrefs.useBerserk == 1)
                    && Me.CurrentTarget.IsWithinMeleeRange),
                Cast("Incarnation", ret => gotTarget
                    && !spellOnCooldown("Incarnation")
                    && buffExists("Berserk", Me)
                    && Me.CurrentTarget.IsWithinMeleeRange),
                Cast("Berserking", ret => gotTarget
                    && SpellManager.HasSpell("Berserking")
                    && LastSpell != "Berserking"
                    && !spellOnCooldown("Berserking")
                    && !HaveHasteBuff
                    && ((CRSettings.myPrefs.useBerserking == 2
                    && IsWoWBoss(Me.CurrentTarget))
                    || CRSettings.myPrefs.useBerserking == 1)
                    && Me.CurrentTarget.IsWithinMeleeRange),
                    
                new Action(ret => { UseTrinket1(); return RunStatus.Failure; }),
                new Action(ret => { UseTrinket2(); return RunStatus.Failure; }),
                new Action(ret => { UseEngiGloves(); return RunStatus.Failure; }),
                new Action(ret => { useVirmenPotion(); return RunStatus.Failure; })
                );
        }
        #endregion CombatBuffs
        #region combat
        public Composite createCombat()
        {
            return new PrioritySelector(
                PauseCR(),
                newTarget(),
                checkRange(5),
                Facing(),
                MeMoving(),
                new Decorator(ret => gotTarget 
                    && Me.Specialization == WoWSpec.DruidFeral && Me.Shapeshift == ShapeshiftForm.Bear, createBearRotation()),
                new Decorator(ret => gotTarget && Me.Shapeshift == ShapeshiftForm.Bear, createBearRotation()),
                new Decorator(ret => gotTarget && addCount >= CRSettings.myPrefs.startCatAoe, createCatAoeRotation()),
                 Cast("Savage Roar", ret => gotTarget
                    && ((Me.ComboPoints == 0 && CRSettings.myPrefs.HaveSavageryGlyph && !debuffExists("Savage Roar", Me))
                    || (Me.ComboPoints > 0 && !debuffExists("Savage Roar", Me)))
                    && Me.EnergyPercent >= 25),
                Cast("Faerie Swarm", ret => gotTarget
                    && !debuffExists("Faerie Swarm", Me.CurrentTarget)
                    && !spellOnCooldown("Faerie Swarm")
                    && Me.CurrentTarget.IsWithinMeleeRange),
                Cast("Rake", ret => gotTarget
                    && !debuffExists("Rake", Me.CurrentTarget)
                    && Me.EnergyPercent >= 35
                    && Me.CurrentTarget.IsWithinMeleeRange),
                Cast("Thrash", ret => gotTarget
                    && LastSpell != "Thrash"
                    && ((!debuffExists("Thrash", Me.CurrentTarget) && buffExists("Clearcasting", Me))
                    || (!debuffExists("Thrash", Me.CurrentTarget) && addCount > 1 && (Me.EnergyPercent >= 50 || buffExists("Clearcasting", Me))))
                    && Me.CurrentTarget.IsWithinMeleeRange),
                Cast("Ravage", ret => gotTarget
                    && buffExists("Incarnation: King of the Jungle", Me)
                    && Me.EnergyPercent >= 45
                    && Me.CurrentTarget.IsWithinMeleeRange),
                Cast("Feral Spirit", ret => gotTarget
                    && SpellManager.HasSpell("Feral Spirit")
                    && !spellOnCooldown("Feral Spirit")
                    && (CRSettings.myPrefs.useFeralSpirit == 1
                    || (CRSettings.myPrefs.useFeralSpirit == 2 && IsWoWBoss(Me.CurrentTarget)))), 
                Cast("Mighty Bash", ret => gotTarget
                    && SpellManager.HasSpell("Mighty Bash")
                    && !spellOnCooldown("Mighty Bash")
                    && Me.CurrentTarget.IsCasting
                    && Me.CanInterruptCurrentSpellCast
                    && Me.CurrentTarget.IsWithinMeleeRange),
                Cast("Skull Bash", ret => gotTarget
                    && !spellOnCooldown("Skull Bash")
                    && Me.CurrentTarget.IsCasting
                    && Me.CanInterruptCurrentSpellCast
                    && Me.CurrentTarget.IsWithinMeleeRange),
                Cast("Healing Touch", ret => gotTarget
                    && buffExists("Predatory Swiftness", Me)
                    && buffTimeLeft("Predatory Swiftness", Me) > 2000
                    && Me.CurrentTarget.IsWithinMeleeRange),
                Cast("Ferocious Bite", ret => gotTarget
                    && ((Me.CurrentTarget.HealthPercent <= 25 && debuffExists("Rip", Me.CurrentTarget) && Me.ComboPoints >= myComboPoints)
                    || (Me.CurrentTarget.HealthPercent > 25 && debuffExists("Rip", Me.CurrentTarget) && Me.ComboPoints >= myComboPoints))
                    && Me.EnergyPercent >= 25
                    && Me.CurrentTarget.IsWithinMeleeRange),
                Cast("Rip", ret => gotTarget
                    && !debuffExists("Rip", Me.CurrentTarget)
                    && Me.EnergyPercent >= 30
                    && Me.ComboPoints >= myComboPoints
                    && Me.CurrentTarget.IsWithinMeleeRange),
                Cast("Mangle", ret => gotTarget
                    && Me.EnergyPercent >= 35
                    && Me.CurrentTarget.IsWithinMeleeRange)
                
                );
        }
        public Composite createBearRotation()
        {
            return new PrioritySelector(

                Cast("Faerie Fire", ret => gotTarget 
                    && !spellOnCooldown("Faerie Fire") 
                    && !debuffExists("Faerie Fire", Me.CurrentTarget) 
                    && !SpellManager.HasSpell("Faerie Swarm")),

                Cast("Faerie Swarm", ret => gotTarget 
                    && SpellManager.HasSpell("Faerie Swarm")
                    && !spellOnCooldown("Faerie Swarm") 
                    && !debuffExists("Faerie Swarm", Me.CurrentTarget)),

                Cast("Thrash", ret => gotTarget 
                    && !spellOnCooldown("Thrash") 
                    && !debuffExists("Thrash", Me.CurrentTarget)
                    && !BearAoe),

                Cast("Maul", ret => gotTarget 
                    && Me.RagePercent > 60),

                Cast("Mangle", ret => gotTarget 
                    && !spellOnCooldown("Mangle")),

                Cast("Swipe", ret => gotTarget 
                    && !spellOnCooldown("Swipe") 
                    && Me.RagePercent >= 15 
                    && addCount > 1
                    && !BearAoe),

                Cast("Lacerate", ret => gotTarget 
                    && !spellOnCooldown("Lacerate"))
                );
        }
        public Composite createCatAoeRotation()
        {
            return new PrioritySelector(
                Cast("Savage Roar", ret => gotTarget
                    && ((Me.ComboPoints == 0 && CRSettings.myPrefs.HaveSavageryGlyph && !buffExists("Savage Roar", Me))
                    || (Me.ComboPoints > 0 && !buffExists("Savage Roar", Me)))
                    && Me.EnergyPercent >= 25),
                Cast("Thrash", ret => gotTarget
                    && LastSpell != "Thrash"
                    && !debuffExists("Thrash", Me.CurrentTarget)
                    && (Me.EnergyPercent >= 50 || buffExists("Clearcasting", Me))
                    && Me.CurrentTarget.IsWithinMeleeRange),
                Cast("Swipe", ret => gotTarget
                    && Me.EnergyPercent >= 45
                    && Me.CurrentTarget.IsWithinMeleeRange)
                );
        }
        public Composite createGuardianRotation()
        {
            return new PrioritySelector(

                );
        }
        #endregion combat

        Composite PauseCR()
        {
            return new Decorator(ret => Paused,
                new Action(ret =>
                    {
                        return;
                    }));
        }
    }
}
